package com.atguigu.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;
/**
 * 商品三级分类
 *
 * @author zhuzhu
 * @email 838759460@qq.com
 * @date 2024-04-28 23:03:03
 */
@RestController
@RequestMapping("product/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * m1查出所有分类的子分类并以树形结构组装
     */
    @RequestMapping("/list/tree")
    //@RequiresPermissions("product:category:list")
    public R list(){
        List<CategoryEntity>entities=categoryService.listWithTree();
        return R.ok().put("data",entities);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    //@RequiresPermissions("product:category:info")
    public R info(@PathVariable("catId") Long catId){
		CategoryEntity category = categoryService.getById(catId);
        return R.ok().put("data", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:category:save")
    public R save(@RequestBody CategoryEntity category){
		categoryService.save(category);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:category:update")
    public R update(@RequestBody CategoryEntity category){
		//categoryService.updateById(category);
        categoryService.updateDetail(category);
        return R.ok();
    }
    /**
     * 批量修改，前端拖拽菜单会用调该接口，拖拽会导致本节点的父节点变化，本节点的层级变化，本节点的排序变化，本节点子节点的层级变化
     * 本节点新放到的上下文环境中的节点的排序也可能会变，所以呢，前端会想办法拿到变了的节点对象，更新他们变了的属性
     * 后端就需要根据id进行字段的更新
     * 前端传过来就类似这样的json{"catId":1},{},{}]
     */
    @RequestMapping("/update/sort")
    //@RequiresPermissions("product:category:update")
    public R update(@RequestBody CategoryEntity[] category){
        categoryService.updateBatchById(Arrays.asList(category));
        return R.ok();
    }
    /**
     * 删除 @RequestBody获取请求体，必须发送post请求
     * springmvc自动将请求体数据（json)转换成对应的对象
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:category:delete")
    public R delete(@RequestBody Long[] catIds){
        //Arrays.asList(catIds)就是把一个数组转成一个集合
		//categoryService.removeByIds(Arrays.asList(catIds));
        //删除之前我们需要检查，删除的菜单不能被别的地方引用
        categoryService.removeByCatids(Arrays.asList(catIds));
        return R.ok();
    }
}
